NAME := "conductor"
VERSION := `git rev-parse HEAD`
SEMVER_VERSION := `grep version Cargo.toml | awk -F"\"" '{print $2}' | head -n 1`
NAMESPACE := "default"
KUBE_VERSION := env_var_or_default('KUBE_VERSION', '1.29.8')
RUST_LOG := "info"

default:
  @just --list --unsorted --color=always | rg -v "    default"

install-traefik:
	kubectl create namespace traefik || true
	helm upgrade --install --namespace=traefik --version=29.0.1 --values=./testdata/traefik-values.yaml traefik traefik/traefik

install-operator:
  just install-cert-manager
  helm upgrade --install --create-namespace --namespace=tembo-system --values=./testdata/operator-values.yaml tembo ../charts/tembo-operator

install-kube-prometheus-stack:
	kubectl create namespace monitoring || true
	helm upgrade --install --namespace=monitoring monitoring prometheus-community/kube-prometheus-stack

install-cert-manager:
  helm upgrade --install \
    cert-manager jetstack/cert-manager \
    --namespace cert-manager \
    --create-namespace \
    --set installCRDs=true

install-tempo:
	helm upgrade --install \
		tempo grafana/tempo \
	  --namespace monitoring

enable-cnpg-default-namespace:
	kubectl label namespace default "tembo-pod-init.tembo.io/watch"="true"
	kubectl delete pods -n tembo-system -l app=tembo-pod-init

update-helm-repos:
	helm repo add cnpg https://cloudnative-pg.github.io/charts
	helm repo add jetstack https://charts.jetstack.io
	helm repo add traefik https://traefik.github.io/charts
	helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
	helm repo add grafana https://grafana.github.io/helm-charts
	helm repo update

# delete kind
delete-kind:
	kind delete cluster && sleep 5

# start kind
start-kind:
	kind delete cluster || true
	kind create cluster --image=kindest/node:v{{KUBE_VERSION}} --config testdata/kind-config.yaml
	just update-helm-repos
	just install-kube-prometheus-stack
	just install-tempo
	just install-traefik
	just install-operator
	just annotate
	kubectl wait pods --for=condition=Ready --timeout=300s --all --all-namespaces

annotate:
	kubectl label namespace {{NAMESPACE}} safe-to-run-coredb-tests=true
	kubectl patch storageclass standard -p '{"allowVolumeExpansion": true}'

fmt:
	cargo clippy --fix
	cargo +nightly fmt

watch:
	POSTGRES_QUEUE_CONNECTION=postgresql://postgres:postgres@0.0.0.0:5431/postgres \
	RUST_BACKTRACE=1 \
	RUST_LOG={{RUST_LOG}} \
	CONTROL_PLANE_EVENTS_QUEUE=myqueue_control_plane \
	DATA_PLANE_EVENTS_QUEUE=myqueue_data_plane \
	METRICS_EVENTS_QUEUE=metrics_events \
	DATA_PLANE_BASEDOMAIN=coredb-development.com \
	CF_TEMPLATE_BUCKET=cdb-plat-use1-dev-eks-data-1-conductor-cf-templates \
	BACKUP_ARCHIVE_BUCKET=cdb-plat-use1-dev-instance-backups \
	STORAGE_ARCHIVE_BUCKET=cdb-plat-use1-dev-instance-storage \
	IS_CLOUD_FORMATION=false \
	IS_GCP=false \
	cargo watch -x run

run-postgres:
	docker rm --force pgmq-pg || true
	docker run -d --name pgmq-pg -e POSTGRES_PASSWORD=postgres -p 5431:5432 quay.io/tembo/pg16-pgmq:v1.3.3

run-tests:
	echo "Running unit tests"
	cargo test -- --nocapture
	echo "Running functional tests"
	cargo test -- --nocapture --ignored
